#include "mydate.h"

static const char *jie_qi[] = {
    ("小寒"), ("大寒"), ("立春"), ("雨水"), ("惊蛰"), ("春分"), ("清明"), ("谷雨"),
    ("立夏"), ("小满"), ("芒种"), ("夏至"), ("小暑"), ("大暑"), ("立秋"), ("处暑"),
    ("白露"), ("秋分"), ("寒露"), ("霜降"), ("立冬"), ("小雪"), ("大雪"), ("冬至")
};

static const char *chinese_days[] =  {
    ("初一"), ("初二"), ("初三"), ("初四"), ("初五"),
    ("初六"), ("初七"), ("初八"), ("初九"), ("初十"),
    ("十一"), ("十二"), ("十三"), ("十四"), ("十五"),
    ("十六"), ("十七"), ("十八"), ("十九"), ("二十"),
    ("廿一"), ("廿二"), ("廿三"), ("廿四"), ("廿五"),
    ("廿六"), ("廿七"), ("廿八"), ("廿九"), ("三十")
};

//static const char *chinese_days[] = {
//    ("正月"), ("二月"), ("三月"), ("四月"),
//    ("五月"), ("六月"), ("七月"), ("八月"),
//    ("九月"), ("十月"), ("冬月"), ("腊月")
//};

MyDate::MyDate(QObject *parent) : QObject(parent)
{

}

MyDate::~MyDate()
{
}


/**
  * 把当天和1900年1月0日（星期日）的差称为积日
  * 以下算式适用于1900年-2100年
  */


//  第y年（1900年算第0年）第x个节气的积日
int MyDate::F(int y, int x)
{
    return int(365.242 * (y - 1900) + 6.2 + 15.22 * x - 1.9 * sin(0.262 * x));
}

////  从1900年开始的第m个朔日的积日
//int M(int m)
//{
//    return int(1.6 + 29.5306 * m + 0.4 * sin(1 - 0.45058 * m));
//}

//  判断闰年
int MyDate::is_leap_year(int year)
{
    return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}

//  获取某年某月的天数
int MyDate::get_days_in_month(int year, int month)
{
    if (is_leap_year(year) && month == 2)
        return days_in_month[month] + 1;
    else
        return days_in_month[month];
}

//  将积日转化为年月日
void MyDate::days_to_gregorian_date(int days)
{
    int year = 1900;
    int month = 1;
    while (days > get_days_in_month(year, month))
    {
        days = days - get_days_in_month(year, month);
        month = month % 12 + 1;
        if (month == 1) year ++ ;
    }

    gregorian_day = days;
    gregorian_month = month;
    gregorian_year = year;
}

//  获取距离某天最近的朔日是第几个朔日，和他的积日
//void MyDate::get_nearest_shuo_day(int day, int &a, int &b)
//{
//    int L = 0;
//    while (day >= int(M(L)))
//    {
//        L = L + 1;
//    }

//    a = L - 1;
//    b = M(L - 1);
//}

//void MyDate::create_jie_qi_list(int year)
//{
//    int dong_zhi_day = F(year - 1, jie_qi[23]);

//}
